# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) 2012 Rami Dabain teamerx@gmail.com
# This program is free software: you can redistribute it and/or modify it 
# under the terms of the GNU General Public License version 3, as published 
# by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
# PURPOSE.  See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE


import gettext, json
import httplib2, urllib,  urllib2
import threading
import cookielib
from gi.repository import Gtk, GObject, Gst
from notify import notify
from login import do_login
from configobj import ConfigObj
from gettext import gettext as _
gettext.textdomain('noc-client')
from netCheck import netCheck

from gi.repository import Gtk, Gdk, GObject # pylint: disable=E0611
import logging
logger = logging.getLogger('noc_client')

from noc_client_lib import Window
from noc_client.AboutNocClientDialog import AboutNocClientDialog
from noc_client.PreferencesNocClientDialog import PreferencesNocClientDialog
from registerPort import registerOkitooPort

# See noc_client_lib.Window.py for more details about how this class works
class NocClientWindow(Window):
    __gtype_name__ = "NocClientWindow"
    
    def finish_initializing(self, builder): # pylint: disable=E1002
        """Set up the main window"""
        super(NocClientWindow, self).finish_initializing(builder)

        # Code for other initialization actions should be added here.
    
        self.btn_login = self.builder.get_object('btn_login')
        self.log_email = self.builder.get_object('log_email')
        self.log_pass = self.builder.get_object('log_pass')
        self.lbl_status = self.builder.get_object('lbl_status')
        self.connect('delete-event', self.hide_window)

    def on_btn_login_clicked(self, widget):         
        #GObject.threads_init()
        
        
        thread = threading.Thread(target=self.bt_login,args=())
        thread.start()
        #thread.run()
        #self.do_login()
        
    def bt_login(self):
        config = ConfigObj('config.dat')
        GObject.idle_add(self.btn_login.set_sensitive,False)
        GObject.idle_add(self.lbl_status.set_text, 'Connecting ...')
        email = self.log_email.get_text()
        passw = self.log_pass.get_text()
        netCheck(do_login,email,passw)
        
        
    def hide_window(window, event, other):
        if Gtk.is_logged == True:
            window.hide()
            return True
        else:
            return False

    def on_mnu_logout_activate(self,widget):
        config = ConfigObj('config.dat')
        config['email'] = ''
        config['passw'] = ''
        config.write()
        Gtk.is_logged = False
        self.show()
